--
-- Author: shunguo.chen
-- Date: 2017-06-15 12:12:11
--
-- HeadRender 对 Base_head.csb 的解析

local AvatarIconNode = require("app.widget.AvatarIconNode")

local ClassRef = app.mvc.createRenderClass(...)

-- UserBaseInfo like
function ClassRef.create(node, type)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.BaseHead))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(nil, type)
	return sprite
end

function ClassRef:init(data, type)
	self.super.init(self, data)

	self:setType(type)
	local mask = self._ui.Panel:getChildByName("mask")
	if mask then mask:setVisible(false) end
	self._avatarIconNode = AvatarIconNode.new(self._ui.Panel, "icon", "mask")

	display.uiAddClick(self._ui.Panel, function(sender)
			executeFunc(self._onClick, sender, self._data)
		end)
	self:refresh()
end

function ClassRef:getPanelSize()
	return self._ui.Panel:getContentSize()
end

-- type=1 	头像款，没有等级和职业
-- type=2 	基础款，有等级和职业
-- type=3   有职业 无等级
function ClassRef:setType(type)
	self._type = type
	if self._type == 1 then
		self._ui.Panel_lv:setVisible(false)
		self._ui.class_icon:setVisible(false)
	elseif self._type == 2 then
		self._ui.Panel_lv:setVisible(true)
		self._ui.class_icon:setVisible(true)
	elseif self._type == 3 then
		self._ui.Panel_lv:setVisible(false)
		self._ui.class_icon:setVisible(true)
	end
end

function ClassRef:refresh()
	self:setIconVisible(self._data ~= nil)
	if self._data then
		self:setType(self._type)
		if not self._data.classID then
			self:_updateAvatarIcon(UD:getAvatarParams())
		else
			self:_updateAvatarIcon(self._data)
			if self._type == 2 or self._type == 3 then
				self._ui.num:setString(self._data.level or self._data.lv)
				self._ui.class_icon:loadTexture(GD:getClassIcon(self._data.classID, 2))
			end
		end
	else
		self._ui.Panel_lv:setVisible(false)
		self._ui.class_icon:setVisible(false)
	end
end

function ClassRef:setAvatarIconFromUrl(url, cacheName)
	if not self._ui.iconNode then
		return
	end
	if not url then
		return
	end
	
	if not string.match(url, "^http.*") then
		return
	end

	if not custom.UrlIconWidget then
		return
	end

	if UD:getHeadShield() then
		return
	end

	self._ui.iconNode:removeAllChildren()
	self._isUrlHead = true
	
	local image = custom.UrlIconWidget:create()
	image:setPosition(0, 0) 
	local size = self._ui.icon:getContentSize()
	self._ui.iconNode:setScale(self._ui.icon:getScale())

	-- 稍微调整一下 + 8 头像裁剪锯齿  让外面的圈盖住锯齿
	local width = size.width + 4
	image:loadFromUrl(url, cacheName)
	image:setScale(width/ 100)
	
	local clipNode = cc.ClippingNode:create()
	local maskSprite = display.newSprite("public/mask_head.png")
	local maskSpriteSize = maskSprite:getContentSize()
    maskSprite:setScale(width/maskSpriteSize.width)

    clipNode:setStencil(maskSprite)
	clipNode:addChild(image)
    clipNode:setAlphaThreshold(0.5)
	clipNode:setInverted(false)
	self._ui.iconNode:addChild(clipNode)
	self:setIconVisible(true)
end

function ClassRef:_updateAvatarIcon(data)
	if self._isUrlHead then
		return
	end
	self._avatarIconNode:update(data)
end


function ClassRef:setIcon(url)
	self:setIconVisible(true)
	self._ui.icon:loadTexture(url)
end

function ClassRef:setIconVisible(b)
	if self._isUrlHead then
		self._ui.icon:setVisible(false)
		if self._ui.iconNode then
			self._ui.iconNode:setVisible(b)
		end
	else
		if self._ui.iconNode then
			self._ui.iconNode:setVisible(false)
		end
		self._ui.icon:setVisible(b)
	end
end

-- 设置头像面向左边，默认是面向右边
function ClassRef:setFaceLeft(b)
	self._ui.icon:setScaleX(b and -1 or 1)
end

function ClassRef:setClassIcon(url)
	self:setClassIconVisible(true)
	self._ui.class_icon:loadTexture(url)
end
function ClassRef:setClassIconVisible(b)
	self._ui.class_icon:setVisible(b)
end

-- 设置品质框
function ClassRef:setQualityIcon( quality )
	quality = quality or 1
	self._ui.bg:loadTexture( Res.Quality_Partner_Icon[quality] )
end

function ClassRef:onClick(cb)
	self._onClick = cb
end

return ClassRef
